package com.siam.package_merchant.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.siam.package_common.constant.Quantity;
import com.siam.package_common.entity.BasicResult;
import com.siam.package_common.exception.StoneCustomerException;
import com.siam.package_merchant.entity.Merchant;
import com.siam.package_merchant.entity.MerchantWithdrawRecord;
import com.siam.package_merchant.service.MerchantService;
import com.siam.package_merchant.service.MerchantWithdrawRecordService;
import com.siam.package_user.entity.MemberTradeRecord;
import com.siam.package_user.feign.MemberTradeRecordFeignApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 提供给其他服务Feign客户端调用的用户模块相关接口
 */
@RestController
public class MerchantWithdrawRecordFeignProvider implements MerchantWithdrawRecordFeignApi {

    @Autowired
    private MerchantWithdrawRecordService merchantWithdrawRecordService;

    @Autowired
    private MemberTradeRecordFeignApi memberTradeRecordFeignApi;

    @Autowired
    private MerchantService merchantService;

    public BasicResult countByAuditStatus(int auditStatus){
        LambdaQueryWrapper<MerchantWithdrawRecord> queryWrapper = new LambdaQueryWrapper<MerchantWithdrawRecord>();
        queryWrapper.eq(MerchantWithdrawRecord::getAuditStatus, auditStatus);
        return BasicResult.success(merchantWithdrawRecordService.count(queryWrapper));
    }

    @Override
    public BasicResult<List<MerchantWithdrawRecord>> queryPaymentProcessingRecord(int merchantId) {
        LambdaQueryWrapper<MerchantWithdrawRecord> merchantQueryWrapper = new LambdaQueryWrapper<>();
        merchantQueryWrapper.eq(MerchantWithdrawRecord::getMerchantId, merchantId);
        merchantQueryWrapper.eq(MerchantWithdrawRecord::getAuditStatus, MerchantWithdrawRecord.AUDIT_STATUS_PAYMENT_PROCESSING);
        List<MerchantWithdrawRecord> withdrawRecordList = merchantWithdrawRecordService.list(merchantQueryWrapper);
        return BasicResult.success(withdrawRecordList);
    }

    @Override
    public BasicResult updateAuditStatusSuccess(String orderNo) {
        // 查询符合条件的记录
        LambdaQueryWrapper<MerchantWithdrawRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MerchantWithdrawRecord::getOrderNo, orderNo);
        queryWrapper.eq(MerchantWithdrawRecord::getAuditStatus, MerchantWithdrawRecord.AUDIT_STATUS_PAYMENT_PROCESSING);
        MerchantWithdrawRecord dbMerchantWithdrawRecord = merchantWithdrawRecordService.getOne(queryWrapper);
        if(dbMerchantWithdrawRecord == null){
            throw new StoneCustomerException("[修改商家提现记录状态]该记录查询为空,orderNo=" + orderNo);
        }

        // 修改审核状态
        MerchantWithdrawRecord updateMerchantWithdrawRecord = new MerchantWithdrawRecord();
        updateMerchantWithdrawRecord.setId(dbMerchantWithdrawRecord.getId());
        updateMerchantWithdrawRecord.setAuditStatus(MerchantWithdrawRecord.AUDIT_STATUS_SUCCESS);
        updateMerchantWithdrawRecord.setAuditTime(new Date());
        updateMerchantWithdrawRecord.setUpdateTime(new Date());
        merchantWithdrawRecordService.updateById(updateMerchantWithdrawRecord);

        //添加平台交易记录
        MemberTradeRecord insertMemberTradeRecord = new MemberTradeRecord();
        insertMemberTradeRecord.setMerchantId(dbMerchantWithdrawRecord.getMerchantId());
        insertMemberTradeRecord.setOutTradeNo(orderNo);
        insertMemberTradeRecord.setType(Quantity.INT_4);
        insertMemberTradeRecord.setPaymentMode(Quantity.INT_1);
        insertMemberTradeRecord.setAmount(dbMerchantWithdrawRecord.getWithdrawAmount());
        insertMemberTradeRecord.setStatus(Quantity.INT_2);
        insertMemberTradeRecord.setCreateTime(new Date());
        insertMemberTradeRecord.setUpdateTime(new Date());
        memberTradeRecordFeignApi.insertSelective(insertMemberTradeRecord);

        //TODO-发消息通知商家

        return BasicResult.success();
    }

    @Override
    public BasicResult updateAuditStatusFail(String orderNo, String auditReason) {
        // 查询符合条件的记录
        LambdaQueryWrapper<MerchantWithdrawRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MerchantWithdrawRecord::getOrderNo, orderNo);
        queryWrapper.eq(MerchantWithdrawRecord::getAuditStatus, MerchantWithdrawRecord.AUDIT_STATUS_PAYMENT_PROCESSING);
        MerchantWithdrawRecord dbMerchantWithdrawRecord = merchantWithdrawRecordService.getOne(queryWrapper);
        if(dbMerchantWithdrawRecord == null){
            throw new StoneCustomerException("[修改商家提现记录状态]该记录查询为空,orderNo=" + orderNo);
        }

        // 修改审核状态
        MerchantWithdrawRecord updateMerchantWithdrawRecord = new MerchantWithdrawRecord();
        updateMerchantWithdrawRecord.setId(dbMerchantWithdrawRecord.getId());
        updateMerchantWithdrawRecord.setAuditStatus(MerchantWithdrawRecord.AUDIT_STATUS_FAIL);
        updateMerchantWithdrawRecord.setAuditReason(auditReason);
        updateMerchantWithdrawRecord.setAuditTime(new Date());
        updateMerchantWithdrawRecord.setUpdateTime(new Date());
        merchantWithdrawRecordService.updateById(updateMerchantWithdrawRecord);

        //添加用户交易记录
        MemberTradeRecord insertMemberTradeRecord = new MemberTradeRecord();
        insertMemberTradeRecord.setMerchantId(dbMerchantWithdrawRecord.getMerchantId());
        insertMemberTradeRecord.setOutTradeNo(orderNo);
        insertMemberTradeRecord.setType(Quantity.INT_4);
        insertMemberTradeRecord.setPaymentMode(Quantity.INT_1);
        insertMemberTradeRecord.setAmount(dbMerchantWithdrawRecord.getWithdrawAmount());
        insertMemberTradeRecord.setStatus(Quantity.INT_3);
        insertMemberTradeRecord.setCreateTime(new Date());
        insertMemberTradeRecord.setUpdateTime(new Date());
        memberTradeRecordFeignApi.insertSelective(insertMemberTradeRecord);

        Merchant dbMerchant = merchantService.selectByPrimaryKey(dbMerchantWithdrawRecord.getMerchantId());

        // 提现失败后置处理
        merchantWithdrawRecordService.handleWithdrawFail(dbMerchantWithdrawRecord, dbMerchant);

        return BasicResult.success();
    }
}